package com.synface.utils.image;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.util.Base64;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageTools
{
  public static int getExifOrientation(String filepath)
  {
    int degree = 0;
    ExifInterface exif = null;
    try {
      exif = new ExifInterface(filepath);
    }
    catch (IOException localIOException) {
    }
    if (exif != null) {
      int orientation = exif.getAttributeInt("Orientation", -1);
      if (orientation != -1)
      {
        switch (orientation)
        {
        case 6:
          degree = 90;
          break;
        case 3:
          degree = 180;
          break;
        case 8:
          degree = 270;
        }

      }

    }

    return degree;
  }

  public static Bitmap rotateBitmap(Bitmap b, String filepath)
  {
    int degrees = getExifOrientation(filepath);
    if ((degrees != 0) && (b != null)) {
      Matrix m = new Matrix();
      m.setRotate(degrees, b.getWidth() / 2.0F, b.getHeight() / 2.0F);
      try {
        Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
        if (b != b2) {
          b.recycle();
          b = b2;
        }
      }
      catch (OutOfMemoryError localOutOfMemoryError) {
      }
    }
    return b;
  }
  public static Bitmap Bytes2Bimap(byte[] bytes) {
    if (bytes.length != 0) {
      return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
    return null;
  }

  public static byte[] Bitmap2Bytes(Bitmap bm)
  {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    return baos.toByteArray();
  }

  public static int getBytesPerPixel(Bitmap.Config config) {
    if (config == Bitmap.Config.ARGB_8888)
      return 4;
    if (config == Bitmap.Config.RGB_565)
      return 2;
    if (config == Bitmap.Config.ARGB_4444)
      return 2;
    if (config == Bitmap.Config.ALPHA_8) {
      return 1;
    }
    return 1;
  }

  public static byte[] File2byte(String filePath) {
    byte[] buffer = null;
    try {
      File file = new File(filePath);
      FileInputStream fis = new FileInputStream(file);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] b = new byte[1024];
      int n;
      while ((n = fis.read(b)) != -1)
      {
        bos.write(b, 0, n);
      }
      fis.close();
      bos.close();
      buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return buffer;
  }

  public static void saveBitmap(String name, Bitmap bm) {
    File f = new File("/storage/sdcard0/", name);
    if (f.exists())
      f.delete();
    try
    {
      FileOutputStream out = new FileOutputStream(f);
      bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
      out.flush();
      out.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static String bitmapToBase64(Bitmap bitmap)
  {
    String result = null;
    ByteArrayOutputStream baos = null;
    try {
      if (bitmap != null) {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        baos.flush();
        baos.close();

        byte[] bitmapBytes = baos.toByteArray();
        result = Base64.encodeToString(bitmapBytes, 2);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (baos != null) {
          baos.flush();
          baos.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  public static void saveToSDCard(String filename, String content) throws Exception {
    File file = new File(Environment.getExternalStorageDirectory(), filename);

    FileOutputStream outStream = new FileOutputStream(file);
    outStream.write(content.getBytes());
    outStream.close();
  }
}